package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ZiXunDaoImpl;
import com.ysd.entity.ZiXun;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminConSultServlet extends BaseServlet {
	//localhost:8080/shangcheng/adminConSult?reqName=getconsult

	public void getconsult(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ZiXunDaoImpl zDao = new ZiXunDaoImpl();
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<ZiXun> zList = zDao.getZiXunList(page, size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson = new Gson();
		String res = gson.toJson(zList);
		out.print(res);
		
		out.flush();
		out.close();
	}
	

		public void getconsultsize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ZiXunDaoImpl zDao = new ZiXunDaoImpl();
			List<ZiXun> zList = zDao.getZiXunList();
			
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			Gson gson = new Gson();
			String res = gson.toJson(zList.size());
			out.print(res);
			
			out.flush();
			out.close();
		}
	public void delxconsult(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int zXXq = Integer.parseInt(request.getParameter("zxfenlei"));		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		ZiXunDaoImpl zDao = new ZiXunDaoImpl();
		int n=zDao.delzX(zXXq);
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}
	
	public void addconsult(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		String zXNames = request.getParameter("zx");
		
		ZiXun z = new ZiXun(0, zXNames);
		
		ZiXunDaoImpl zDao = new ZiXunDaoImpl();
		int n = zDao.addConSult(z);
		if(n > 0) {
			out.print(Result.toClient(true, "添加成功",""));
		}else {
			out.print(Result.toClient(false, "添加失败",""));
		}
		out.flush();
		out.close();
	}
	public void xgconsult(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		
		ZiXunDaoImpl zDao = new ZiXunDaoImpl();
		int xgid = Integer.parseInt(request.getParameter("zid"));
		 ZiXun xg = zDao.getZiXun(xgid);
			
		Gson gson = new Gson();
		String res = gson.toJson(xg);
		out.print(res);	
		
		
		out.flush();
		out.close();
	}
	
	/**
	 * 批量删除
	 * localhost:8080/shangcheng/adminConSult?reqName=deletezXFEbLeiIDs
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
public void deletezXFEbLeiIDs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		
		ZiXunDaoImpl zDao = new ZiXunDaoImpl();
		String zXFEbLeiIDs = request.getParameter("zXFEbLeiIDs");
		int n = zDao.deletezXFEbLeiIDs(zXFEbLeiIDs);
			
		if(n>0){
			out.print(Result.toClient(true,"删除成功",""));
			} else {
			out.print(Result.toClient(false,"删除失败",""));
			}

		out.flush();
		out.close();
	}
	
public void updateconsult(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		
		ZiXunDaoImpl zDao = new ZiXunDaoImpl();
		int zXFEbLeiID =0;
		if(request.getParameter("zXFEbLeiID")!=null && !request.getParameter("zXFEbLeiID").equals("")) {
		zXFEbLeiID = Integer.parseInt(request.getParameter("zXFEbLeiID"));
		}
		String zXNames = request.getParameter("zXNames");
		List<ZiXun> zList = zDao.getFenLeiByName(zXNames);
		if (zList.size()==0) {
			ZiXun zx = new ZiXun(zXFEbLeiID, zXNames);
			System.out.print(zx);
			int n= zDao.updateconsult(zx);
			if(n>0) {
				out.printf(Result.toClient(true,"修改成功",""));
			}else {
				out.printf(Result.toClient(false,"修改失败",""));
			}
		}else {
			out.print(Result.toClient(false, "分类名已存在",""));

		}


		
		
		out.flush();
		out.close();
	}
}


